package com.lock.subscribe.offlinepwd;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lock.subscribe.util.SoUtil;

public class OfflinePasswordSo {
	private static Logger logger = LoggerFactory.getLogger(OfflinePasswordSo.class);
	public final static Integer minDuration = 300;
	public final static Integer threeDayDuration = 4115;
	public final static Integer ninetythreeDayDuration = 126172;
	public final static Integer oneyearDuration = 501943;


	public static long fiveMinPwd(long current_time, String mac, long startTime) {
		char pwdType = '0';
		String macs = getChangeMac(mac, current_time);
		char[] macc = getCharMacArray(macs);
		long start_tim = ((startTime - current_time) / minDuration) / 63;
		long duration = ((startTime - current_time) / minDuration) % 63;
		long pwd = SoUtil.invoke(pwdType, macc, current_time, start_tim, duration);
		logger.error("mac:" + macs + "  0:duration:" + duration + "-----" + start_tim + "---pwd:" + pwd
				+ "current_time:" + current_time + "----" + "startTime:" + startTime);
		return pwd;
	}

	public static long threeDayPwd(long current_time, String mac, long startTime, long endTime) {
		char pwdType = '1';
		String macs = getChangeMac(mac, current_time);
		char[] macc = getCharMacArray(macs);
//		Map<String, Long> dp = getDuration(current_time,startTime,endTime,threeDayDuration);
//		long start_tim=(dp.get("startTime")-current_time)/threeDayDuration;
//		long duration=(dp.get("endTime")-dp.get("startTime"))/threeDayDuration;
		long dura = (endTime-startTime)%threeDayDuration;
		startTime = startTime - dura;
		long start_tim=(startTime-current_time)/threeDayDuration;
		long duration=(endTime-startTime)/threeDayDuration;
		long pwd = SoUtil.invoke(pwdType, macc, current_time, start_tim, duration);
		logger.error("pwdType:-----"+pwdType+"-----macs:" + macs + "-----duration:" + duration + "-----" + start_tim
				+ "current_time:" + current_time + "----" + "startTime:" + startTime + "----" + "endTime:" + endTime + "---pwd:" + pwd);
		return pwd;
	}

	public static long ninetythreeDayPwd(long current_time, String mac, long startTime, long endTime) {
		char pwdType = '2';
		String macs = getChangeMac(mac, current_time);
		char[] macc = getCharMacArray(macs);
//		long start_tim = (startTime - current_time) / threeDayDuration;
//		long duration = (endTime - startTime)% ninetythreeDayDuration;
		Map<String, Long> dp = getDuration(current_time,startTime,endTime,ninetythreeDayDuration);
		long start_tim=(dp.get("startTime")-current_time)/threeDayDuration;
		long duration=(dp.get("endTime")-dp.get("startTime"))/ ninetythreeDayDuration;
		long pwd = SoUtil.invoke(pwdType, macc, current_time, start_tim, duration);
		logger.error("pwdType:-----"+pwdType+"-----macs:" + macs + "-----duration:" + duration + "-----" + start_tim
				+ "current_time:" + current_time + "----" + "startTime:" + startTime + "----" + "endTime:" + endTime + "---pwd:" + pwd);
		return pwd;
	}

	public static long oneyearPwd(long current_time, String mac, long startTime, long endTime) {
		char pwdType = '3';
		String macs = getChangeMac(mac, current_time);
		char[] macc = getCharMacArray(macs);
//		long start_tim = (startTime - current_time) / threeDayDuration;
//		long duration = (endTime - startTime)% oneyearDuration;
		Map<String, Long> dp = getDuration(current_time,startTime,endTime,oneyearDuration);
		long start_tim=(dp.get("startTime")-current_time)/threeDayDuration;
		long duration=(dp.get("endTime")-dp.get("startTime"))/oneyearDuration;
		long pwd = SoUtil.invoke(pwdType, macc, current_time, start_tim, duration);
		logger.error("pwdType:-----"+pwdType+"-----macs:" + macs + "-----duration:" + duration + "-----" + start_tim
				+ "current_time:" + current_time + "----" + "startTime:" + startTime + "----" + "endTime:" + endTime + "---pwd:" + pwd);
		return pwd;
	}

	public static char[] getCharMacArray(String macString) {
		String[] macParts = macString.split(":");
		char[] mac = new char[macParts.length];
		for (int i = 0; i < macParts.length; i++) {
			mac[i] = (char) Integer.parseInt(macParts[i], 16);
		}
		return mac;
	}
	public static String getChangeMac(String mac, long timeStamp) {
		String result = "";
		String hexTime = Long.toHexString(timeStamp);
		String last = hexTime.substring(hexTime.length() - 2, hexTime.length()).toUpperCase();
		String second = hexTime.substring(hexTime.length() - 4, hexTime.length() - 2).toUpperCase();
		String[] macc = mac.split(":");
		macc[0]= second;
		macc[1]= last;
		result =String.join(":",macc);
		return result;
	}
	public static Map<String, Long> getDuration(long ctime, long stime, long etime, Integer num) {
		Map<String, Long> result = new HashMap<String, Long>();
		long s = 0;
		long d = 0;
		long c = (etime - stime) % num;
		if (c < num * 1 / 8) {
			result.put("startTime", stime);
			result.put("endTime", etime);
			return result;
		} else if (stime < ctime) {
			c = (etime - ctime) % num;
			if(c > num * 1 / 6) {
				etime = etime + (num - (etime - ctime) % num);	
			}
			result.put("startTime", ctime);
			result.put("endTime", etime);
			return result;
		}
		s = stime - c;
		if(s < ctime) {
			d = (etime - ctime) % num;
			if(d > num * 1 / 6) {
				etime = etime + (num - (etime - ctime) % num);	
			}
			result.put("startTime", ctime);
			result.put("endTime", etime);
			return result;
		}
//		long sc = threeDayDuration - (s - ctime) % threeDayDuration;
//		stime = s - sc;
		result.put("startTime", stime);
		result.put("endTime", etime);
		return result;
	}

}
